﻿using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace Web
{
    public partial class Goods : System.Web.UI.Page
    {
        public int Count;
        public int Count1 = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                goodsType();
                UserNum();
                GoodsAll(0, "");
                Zys();
                Label6.Text = 1.ToString();
            }
        }
       
        void GoodsAll(int ID,String GoodsName)
        {
           
           
            GridView2.DataSource = BackstagePaging.GoodsAll1(ID,GoodsName);
          
            GridView2.DataBind();
        }
        //搜索模糊查询
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (BackstagePaging.GoodsAll1(0,TextBox1.Text).Rows.Count>0)
            {
                 GoodsAll(0,TextBox1.Text);
            }
            else
            {
                GoodsAll(0,"");
                Response.Write("<script>alert('没有数据')</script>");
               
            }
        }
        void goodsType()
        {
            this.DropDownList1.DataSource = UsersDAL.GoodsType();
            this.DropDownList1.DataTextField = "GoodsTypeName";
            this.DropDownList1.DataValueField = "GoodsTypeID";
            DataBind();
        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (UsersBLL.GoodsDelet(int.Parse(this.GridView2.Rows[e.RowIndex].Cells[0].Text)))
            {
                
                UserNum();
                GoodsAll(0,"");
                Response.Write("<script>alert('删除成功')</script>");

            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
        }

        private void UserNum()
        {
            int a = UsersBLL.Goodsalll();
            Label1.Text = "商品数据有" + a + "条";
        }
        //点击编辑
        
        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView2.EditIndex = e.NewEditIndex;
            GoodsAll(0,"");
        }
        //取消编辑
        protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView2.EditIndex = -1;
            GoodsAll(0, "");
        }

        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Models.Goods gs = new Models.Goods();
            gs.GoodsID= int.Parse(((TextBox)GridView2.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            gs.GoodsName= ((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            gs.GoodsTypeID= int.Parse(DropDownList1.SelectedValue);
            gs.GoodsPrice=decimal.Parse(((TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
            gs.GoodsDesc= ((TextBox)GridView2.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            gs.GoodsNum= int.Parse(((TextBox)GridView2.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
           
            gs.CreateTime=DateTime.Parse(((TextBox)GridView2.Rows[e.RowIndex].Cells[6].Controls[0]).Text);
            if (UsersBLL.GoodsUpda(gs))
            {
                Response.Write("<script>alert('修改成功')</script>");
                GridView2.EditIndex = -1;
                GoodsAll(0, "");
            }
            else
            {
                Response.Write("<script>alert('修改失败')</script>");
            }
        }

       
        //Sql语句分页
        //总页数
        public void Zys()
        {
            Label8.Text = Convert.ToString(UsersBLL.Goodsalll());

            Count = int.Parse(Label8.Text) % 5;
            if (Count == 0)
            {
                Label8.Text = (int.Parse(Label8.Text) / 5).ToString();
            }
            else
            {
                Label8.Text = (int.Parse(Label8.Text) / 5 + 1).ToString();
            }
        }
        //首页
        protected void Button1_Click(object sender, EventArgs e)
        {
            GoodsAll(0, "");
            Count1 = 0;
            Label6.Text = Convert.ToString(1);
        }
        //上一页
        protected void Button2_Click(object sender, EventArgs e)
        {


            if (int.Parse(Label6.Text) > 1)
            {

                Count1 = int.Parse(Label6.Text) - 1;
                Label6.Text = Count1.ToString();

                GoodsAll((Count1 - 1) * 5, "");
            }
            else
            {
                Response.Write("<script>alert('翻页失败')</script>");
            }
        }
        //下一页
        protected void Button5_Click(object sender, EventArgs e)
        {

            if (int.Parse(Label6.Text) <= int.Parse(Label8.Text) - 1)
            {
                Count1 = int.Parse(Label6.Text);
                GoodsAll(Count1 * 5, "");

                Label6.Text = (Count1 + 1).ToString();






            }
            else
            {
                Response.Write("<script>alert('翻页失败')</script>");
            }



        }
        //尾页

        protected void Button4_Click(object sender, EventArgs e)
        {
            GoodsAll((int.Parse(Label8.Text) - 1) * 5, "");
            Label6.Text = Label8.Text;
        }
        //输入跳转
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (int.Parse(TextBox2.Text.Trim()) > 1 && int.Parse(TextBox2.Text.Trim()) <= int.Parse(Label8.Text) &&TextBox2.Text.Trim()!=null)
            {
                GoodsAll((int.Parse(TextBox2.Text.Trim()) - 1) * 5, "");
                Label6.Text = TextBox2.Text;
            }
            else
            {
                Response.Write("<script>alert('输入的页数无效')</script>");
            }
        }
    }
}